<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use backend\models\Roles;
use backend\models\Departments;
use common\models\Admin;
use yii\data\Pagination;
use backend\models\RoleGrants;
use backend\models\GrantsCats;

/**
 * Clue controller
 */
class SystemController extends Controller
{
    public $enableCsrfValidation = false;//开启防止crf攻击
    //public $layout='base';
    /**
     * @inheritdoc
     */

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * 参数设置
     */
    public function actionIndex()
    {
        $userInfo=\Yii::$app->session->get('userInfo');
        return $this->render('index',$userInfo);
    }
    /**
     * 部门列表
     */
    public function actionDepartmentsList(){
       $departmentsList=Departments::getAllDeparmentList();
        return $this->renderPartial('department-list',['departmentsList'=>$departmentsList]);
    }



    /**
     * 新增部门跳转页面
     */
    public function actionToAddDepartment(){
        $departmentsList=Departments::getDepartmentsList();
        return $this->render('add-department',['departmentsList'=>$departmentsList]);
    }

    /**
     * 新增和修改部门操作
     */
    public function actionAddDepartmented(){
        $params=\Yii::$app->request->post();

        $parentId=$params['departmentId'];
        $departName=$params['departName'];
        $parentInfo=Departments::findOne(['id'=>$parentId])->toArray();
        $checkName=Departments::findOne(['departName'=>$departName]);

        $id=$params['theId'];
        if($id>0){
            $department=Departments::findOne(['id'=>$id]);
            if(empty($department)){
                returnResult(100031,'找不到该部门');
            }
        }else{
            if(!empty($checkName)){
                returnResult(100030,'部门名称已存在');
            }
            $department=new Departments();
        }
        $department->departName=$departName;
        $department->parentId=$parentId;
       $department->departPcount=0;
        $department->isDelete=0;
        $department->isEnable=1;
        $department->grade=$parentInfo['grade']+1;
        $doResult=$department->save();

        if($doResult){
            returnResult(0,'操作成功');
            window.location.reload();
        }else{
            returnResult(0,'操作失败');
        }
    }
    /**
     * 部门删除
     */
    public function actionDeleteDepartment(){
        $department=Departments::findIdentity(7);
        $department->delete();

    }


    /**
     *角色列表
     */
    public function actionRolesList(){
        $rolesList=Roles::getRolesList();
        $grantsCatsList=GrantsCats::getGrantsCatsList();
        $roleGrantsList=RoleGrants::getRoleGrantsList();
        $data=array();
        $data['rolesList']=$rolesList;
        $data['grantsCatsList']=$grantsCatsList;
        $data['roleGrantsList']=$roleGrantsList;
        return $this->renderPartial('role-list',$data);
    }


    /**
     * 新怎角色操作
     */
    public function actionEditRoled(){
        $params=Yii::$app->request->post();

        $roleName=getData($params['roleName'],'填写角色名称');
        $granted=implode(',',$params['grants']);
        $id=$params['roleId'];

        $checkRoleName=Roles::findOne(['roleName'=>$roleName]);

        if($id>0){
            $roles=Roles::findOne(['id'=>$id]);
            if(empty($roles)){
                returnResult(100017,'要修改的角色不存在');
            }
        }else{
            if(!empty($checkRoleName)){
                returnResult(100016,'角色名已经存在');
            }
            $roles=new Roles();
        }

        $roles->roleName=$roleName;
        $roles->granted=$granted;
        $roles->remarks=$params['remarks'];
        $roles->canDelete=1;
      /*  $roles->roleCount=0;

        $roles->isDelete=0;
        $roles->isEnable=0;*/
        $doResult=$roles->save();
        if(!empty($doResult)){
            returnResult(0,'操作角色成功');
        }else{
            returnResult(100015,'操作角色失败');
        }


    }
    /**
     * 删除角色列表
     */
    public function actionDelRole(){
        $params=Yii::$app->request->post();
        $role=Roles::findOne(['id'=>$params['roleId']]);
        if(empty($role)){
            returnResult(100057,'角色记录不存在');
        }
        $role->isDelete=1;
        $doResult=$role->save();
        if($doResult){
            returnResult(0,'删除成功');
        }else{
            returnResult(100060,'删除失败');
        }
    }
    /**
     * 员工列表
     */
    public function actionUserList(){
      //  $userList=Admin::getUserList();

        $userList=Admin::find()->joinWith('departments')->joinWith('roles');
        $departmentsList=Departments::getAllDeparmentList();
        $pagination=new Pagination(['totalCount'=>$userList->count()]);
        $pagination->setPageSize(2);
        $userList=$userList->offset($pagination->offset)->limit($pagination->limit)->all();

        $data=array('userList'=>$userList,'pagination'=>$pagination);
        $data['departmentsList']=$departmentsList;
        $data['rolesList']=Roles::getRolesList();




        return $this->renderPartial('user-list',$data);
    }
    /**
     * 员工列表页面
     */
    public function actionToAddUser(){
        return $this->render('add-users');
    }
    /**
     * 添加员工
     */
    public function actionAddUsered(){

        $params=Yii::$app->request->post();
        $id=$params['theId'];
        $userName=$params['userName'];
        $departmentId=$params['departmentId'];
        $roleId=$params['roleId'];
        $cellPhone=$params['cellPhone'];
        $email=$params['email'];
        $password=$params['password'];
        $checkUserName=Admin::find()->where(['userName'=>$userName])->one();

        if($id>0){
            $users=Admin::findOne(['id'=>$id]);
        }else{
            if(!empty($checkUserName)){
                returnResult(100012,'该账户名已存在');
            }
            $users=new Admin();
        }
        $users->userName=$userName;
        $users->password=md5($password);
        $users->roleId=$roleId;
        $users->addTime=time();
        $users->departmentId=$departmentId;
        $users->cellPhone=$cellPhone;
        $users->email=$email;
        $users->clueCount=0;
        $users->custumerCount=0;
        $users->agentCount=0;
        $doResult=$users->save();
        if($doResult){
           returnResult(0,'操作成功');
        }else{
            returnResult(100010,'操作失败');
        }
    }
    /**
     * 编辑员工
     */
    public function actionEditUsered(){

    }
    /**
     * 通过部门Id获取员工列表
     */
    public function actionDepartmentList(){
        $params=Yii::$app->request->post();
        $departmentId=getData($params['departmentId'],'参数不足');
        $departmentList=Admin::getDepartUserList($departmentId);
        ajaxReturn($departmentList);

    }
    /**
     * 删除部门
     */
    public function actionDelDepartment(){
        $params=Yii::$app->request->post();
        $departmentId=getData($params['departmentId'],'参数错误，请联系管理员');
        $department=Departments::findOne(['id'=>$departmentId]);
        if(empty($department)){
            returnResult(100050,'找不到该记录');
        }
        $department->isDelete=1;
        $doResult=$department->save();
        if($doResult){
            returnResult(0,删除成功);
        }else{
            returnResult(100054,'删除失败');
        }
    }



}
